# # -*- coding:utf-8 -*-
# """Created By Corvo"""
#
# __version__ = 2
#
# import os
# import random
# import re
# import uuid
#
#
# class AddonOperator(object):
#     Root = "../../../../"
#     BehRoot = "../../../"
#
#     @classmethod
#     def GetManifest(cls):
#         """获得配置文件"""
#         targetList = []
#         for dirName in os.listdir(cls.Root):
#             dirPath = cls.Root + dirName
#             if not os.path.isdir(dirPath):
#                 continue
#             for cldName in os.listdir(dirPath):
#                 if "manifest" in cldName:
#                     targetList.append(dirPath + "/%s" % cldName)
#         return targetList
#
#     @classmethod
#     def ChangeUUID(cls, version=None):
#         # type: (str) -> None
#         """改变附加包的UUID"""
#         for targetFile in cls.GetManifest():
#             with open(targetFile, "r") as f:
#                 context = "".join(f.readlines())
#                 context = re.sub(r"\"uuid\":\s+\"(.*)\"", lambda x: "\"uuid\": \"%s\"" % uuid.uuid4(), context)
#                 if version:
#                     number = version.split(".")
#                     context = re.sub(r"\"version\": \[.*?]",
#                                      lambda x: "\"version\": [%s, %s, %s]" % (number[0], number[1], number[2]), context)
#             with open(targetFile, "w") as g:
#                 g.write(context)
#
#     @classmethod
#     def GetRandomSpawnEggColor(cls):
#         """返回列表 包含两个生物蛋的颜色十六进制"""
#         cList = list("a1b2c3d4e5f67890")
#         rList = []
#         for _ in xrange(2):
#             rList.append("".join([random.choice(cList) for _ in xrange(6)]))
#         return rList
#
#     @classmethod
#     def ChangeSpawnEggColor(cls, resPath=None):
#         """
#         修改资源包所有生物的怪物蛋\n
#         默认修改行为包所有生物\n
#         填路径则仅该文件夹
#         """
#         if not resPath:
#             for target in os.listdir(cls.Root):
#                 newPath = cls.Root + target
#                 if os.path.isdir(newPath) and "entity" in os.listdir(newPath):
#                     resPath = newPath
#                     break
#         print("[+] 当前工作路径：%s" % resPath)
#         count = 0
#         for root, _, files in os.walk(resPath):
#             for fileName in files:
#                 if not fileName.split(".")[-1] == "json":
#                     continue
#                 target = os.path.join(root, fileName)
#                 with open(target, "r") as f:
#                     context = "".join(f.readlines())
#                 if not re.findall(r"base_color|overlay_color", context):
#                     continue
#                 print("[+] 正在处理文件：%s" % target.replace(cls.Root, ""))
#                 rList = cls.GetRandomSpawnEggColor()
#                 context = re.sub("\"base_color\": \"#(.*?)\"", "\"base_color\": \"#%s\"" % rList[0], context)
#                 context = re.sub("\"overlay_color\": \"#(.*?)\"", "\"overlay_color\": \"#%s\"" % rList[1], context)
#                 with open(target, "w") as g:
#                     g.write(context)
#                 count += 1
#         print("[+] 执行完毕 共处理文件数量：%s" % count)
#
#     @classmethod
#     def ChangeModVersion(cls):
#         """修改模组版本"""
#         path = "../../../modCommon/modConfig.py"
#         new_version = None
#         with open(path, "r") as f:
#             context = "".join(f.readlines())
#             version = re.findall("modVersion = \"(.*?)\"", context)
#             if version:
#                 target = version[0]  # type: str
#                 number = target.split(".")
#                 new_index = int(number[-1]) + 1
#                 number[-1] = str(new_index)
#                 new_version = ".".join(number)
#                 context = context.replace(target, new_version)
#                 with open(path, "w") as g:
#                     g.write(context)
#         return new_version
#
#     @classmethod
#     def ExportPack(cls):
#         """输出压缩包"""
#         from zipfile import ZipFile
#         version = cls.ChangeModVersion()
#         cls.ChangeUUID(version)
#         root_list = [path for path in os.listdir(cls.Root) if re.findall("Res|Beh", path)]
#         modName = re.sub("Res|Beh", "", root_list[0])
#         # -----------------------------------------------------------------------------------
#         export_name = "%s.zip" % modName
#         export_path = cls.Root + export_name
#         ban_dir = [".idea", "doc"]
#         ban_file = [export_name, "addon.py"]
#         # -----------------------------------------------------------------------------------
#         handle = ZipFile(export_path, "w")
#         for target in root_list:
#             for root, dirs, files in os.walk(cls.Root + target):
#                 dirs[:] = [x for x in dirs if x not in ban_dir]
#                 files = [file_name for file_name in files if file_name not in ban_file]
#                 for file_name in files:
#                     head = root.replace(cls.Root, "")
#                     path = os.path.join(root, file_name)
#                     handle.write(path, head + os.sep + file_name)
#         handle.close()
#
#     @classmethod
#     def ModifyNewVersion(cls):
#         path_list = ["Presets", "Parts"]
#         for path in path_list:
#             if not os.path.isdir(cls.BehRoot + path):
#                 os.makedirs(cls.BehRoot + path)
#
#
# if __name__ == '__main__':
#     """测试"""
#     AddonOperator.ChangeUUID()
#     # AddonOperator.ModifyNewVersion()
#     # AddonOperator.ChangeSpawnEggColor()
#     # AddonOperator.ExportPack()
#     print "[+] 执行完成"
